package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleSearchDao;
import com.tensquare.search.pojo.Article;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * Created by WF on 2019/9/20 10:11
 */
@Service
public class ArticleSearchService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ArticleSearchDao articleSearchDao;
    //1.添加文章
    public void add(Article article) {
        //1.1）为文章设置主键
        long id = idWorker.nextId();
        article.setId(id + "");
        //1.2)保存文章到索引库中
        articleSearchDao.save(article);
    }
    //2.根据标题及内容进行分页查询
    public PageResult<Article> findByTitelAndContentLike(String keywords, int page, int size) {
        //2.1)查询得到分页内容
        Page<Article> articlePage = articleSearchDao.findByTitleOrContentLike(keywords,keywords,PageRequest.of(page-1,size));
        //2.2)将分页对象转换为PageResult对象
        return new PageResult<>(articlePage.getTotalElements(),articlePage.getContent());
    }
}
